package com.attendance.manage.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import com.attendance.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.attendance.manage.mapper.AttendanceMapper;
import com.attendance.manage.domain.Attendance;
import com.attendance.manage.service.IAttendanceService;

/**
 * 考勤记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-01
 */
@Service
public class AttendanceServiceImpl implements IAttendanceService
{
    @Autowired
    private AttendanceMapper attendanceMapper;

    /**
     * 查询考勤记录
     *
     * @param id 考勤记录主键
     * @return 考勤记录
     */
    @Override
    public Attendance selectAttendanceById(Long id)
    {
        return attendanceMapper.selectAttendanceById(id);
    }

    /**
     * 查询考勤记录列表
     *
     * @param attendance 考勤记录
     * @return 考勤记录
     */
    @Override
    public List<Attendance> selectAttendanceList(Attendance attendance)
    {
        return attendanceMapper.selectAttendanceList(attendance);
    }

    /**
     * 新增考勤记录
     *
     * @param attendance 考勤记录
     * @return 结果
     */
    @Override
    public int insertAttendance(Attendance attendance)
    {

        //设置当前日期只要 日期 不包含时间
        LocalDate today = LocalDate.now();
        // 如需转回 Date 类型
        Date dateOnly = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        attendance.setCreateDate(dateOnly);
        //判断今天是否已经打卡
        if(!selectAttendanceList(attendance).isEmpty())
        {
            System.out.println(attendance);
            return 10000;
        }
        //判断现在的时间是否在9点之后
        if (DateUtils.getNowDate().getHours() > 9)
        {
            attendance.setClockInTime(DateUtils.getNowDate());
            attendance.setStatus("迟到");
            System.out.println( attendance);
            return attendanceMapper.insertAttendance(attendance);
        }
        else {
            attendance.setClockInTime(DateUtils.getNowDate());
            attendance.setStatus("正常");
            System.out.println( attendance);
            return attendanceMapper.insertAttendance(attendance);
        }
    }

    /**
     * 修改考勤记录
     *
     * @param attendance 考勤记录
     * @return 结果
     */
    @Override
    public int updateAttendance(Attendance attendance)
    {
        //设置当前日期只要 日期 不包含时间
        LocalDate today = LocalDate.now();
        // 如需转回 Date 类型
        Date dateOnly = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        attendance.setCreateDate(dateOnly);
        //判断今天是否已经打卡
        if(selectAttendanceList(attendance).isEmpty())
        {
            System.out.println(attendance);
            return 10000;
        }
        //判断今天是否已经签退
        if(selectAttendanceList( attendance).get(0).getClockOutTime() != null)
        {
            System.out.println(selectAttendanceList( attendance).get(0).getClockOutTime());
            return 10001;
        }
        if(DateUtils.getNowDate().getHours() < 18){
           if(selectAttendanceList( attendance).get(0).getStatus().equals("迟到")){
               attendance.setStatus("旷工");
               attendance.setClockOutTime(DateUtils.getNowDate());
               System.out.println( attendance);
               return attendanceMapper.updateAttendance(attendance);
           }
            attendance.setClockOutTime(DateUtils.getNowDate());
            attendance.setStatus("早退");
            System.out.println( attendance);
            return attendanceMapper.updateAttendance(attendance);
        }
        attendance.setClockOutTime(DateUtils.getNowDate());
        return attendanceMapper.updateAttendance(attendance);
    }

    /**
     * 批量删除考勤记录
     *
     * @param ids 需要删除的考勤记录主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceByIds(Long[] ids)
    {
        return attendanceMapper.deleteAttendanceByIds(ids);
    }

    /**
     * 删除考勤记录信息
     *
     * @param id 考勤记录主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceById(Long id)
    {
        return attendanceMapper.deleteAttendanceById(id);
    }
}
